package ElectricCraft.frames;

import ElectricCraft.misc.PopupMenuWrapper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * EditorsTabPanel class.
 */
public class EditorsTabPanel extends JTabbedPane {
    private final MainWindow parent;
    public EditorsTabPanel(MainWindow parent) {
        this.parent = parent;

        initPopupMenu();
    }

    private PopupMenuWrapper popupMenu;
    private void initPopupMenu() {
        popupMenu = new PopupMenuWrapper();

        popupMenu.createItems("Save", "Close", "Close Others", "Close All");

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (popupMenu.checkAction(e, "Save")) {
                    processSave();
                } else if (popupMenu.checkAction(e, "Close")) {
                    processClose(true, false);
                } else if (popupMenu.checkAction(e, "Close Others")) {
                    processClose(false, true);
                } else if (popupMenu.checkAction(e, "Close All")) {
                    processClose(true, true);
                }
            }
        };

        popupMenu.addActionListener(listener);
    }

    public ChipEditorPane getCurrentEditor() {
        Component component = getSelectedComponent();
        if (component != null && component instanceof ChipEditorPane) {
            return (ChipEditorPane)component;
        }
        return null;
    }


    private void processSave() {
        ChipEditorPane editor = getCurrentEditor();
        if (editor == null) {
            return ;
        }

        editor.save();
        parent.savePackage(false);
    }

    private void processClose(boolean closeSelf, boolean closeOthers) {
        ChipEditorPane currentEditor = getCurrentEditor();

        List<ChipEditorPane> openedEditors = parent.getOpenedEditors();
        ChipEditorPane [] editors = new ChipEditorPane[openedEditors.size()];
        openedEditors.toArray(editors);

        for (ChipEditorPane editor : editors) {
            if (closeSelf && editor == currentEditor) {
                parent.closeEditor(editor);
            }
            if (closeOthers && editor != currentEditor) {
                parent.closeEditor(editor);
            }
        }
    }

    @Override
    public void addTab(String title, final Component component) {
        super.addTab(title, component);

        JLabel label = new JLabel(title);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                setSelectedComponent(component);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                if (!e.isConsumed() && e.getButton() == MouseEvent.BUTTON3) {
                    e.consume();

                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        int index = indexOfComponent(component);
        setTabComponentAt(index, label);
        setSelectedIndex(index);
    }

    @Override
    public void setTitleAt(int index, String title) {
        JLabel label = (JLabel)getTabComponentAt(index);
        label.setText(title);
    }
}
